package com.bah.tract.client.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TimeCardTableModel extends AbstractTableModel {

	private static final int END_COLUMN = 3;

	private static final int START_COLUMN = 2;

	private static final int COLUMN_COUNT = 5;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final String[] columns = new String[] { "Project", "Task", "Start",
			"End", "Comment" };

	private final List<TimeCardRow> data;

	private final DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

	public TimeCardTableModel(final List<TimeCardRow> sortedSet) {
		data = sortedSet;
	}

	@Override
	public final String getColumnName(final int column) {
		return columns[column];
	}

	@Override
	public final Object getValueAt(final int row, final int column) {
		final TimeCardRow rowData = data.get(row);
		switch (column) {
		case 0:
			return rowData.getProject();
		case 1:
			return rowData.getTask();
		case START_COLUMN:
			return getTime(rowData.getStart());
		case END_COLUMN:
			return getTime(rowData.getEnd());
		default:
			return rowData.getComment();
		}
	}

	private String getTime(final Date date) {
		if (date == null) {
			return null;
		}
		return dateFormat.format(date);
	}

	@Override
	public final int getRowCount() {
		return data.size();
	}

	@Override
	public final int getColumnCount() {
		return COLUMN_COUNT;
	}

	public final TimeCardRow getRow(final int selectedRow) {
		return data.get(selectedRow);
	}

}
